#include<bits/stdc++.h>
using namespace std;

int a[50000]; 

int main()
{
	for(int i = 2;i <= 11000;i++)
	{
		int k = i/2;
		for(int j = 2;j <= k;j++)
			if(i%j == 0)
				a[i]++;
	}
	int n;
	cin>>n;
	while(n--)
	{
		int l, r;
		cin>>l>>r;
		int idx = max_element(a+l, a+r+1)-a;
		if(a[idx])
			cout<<idx<<endl;
		else
			cout<<"-1"<<endl;
	}
    return 0;
}

